package com.cloudkinto.controller.finance;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.CompanyAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@RestController
@Api(tags = "财务模块-财务报表")
@RequestMapping("/api/admin/account")
public class CompanyAccountController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CompanyAccountController.class);

    @Autowired
    private CompanyAccountService service;

    @UserSessionRequired
    @GetMapping("/accountUsable")
    @ApiOperation(value = "账户是否可用", notes = "账户是否可用")
    public SingleResult getIsDue() {
        return SingleResult.success(service.getIsDue(getCompanyId()));
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams(@ApiImplicitParam(value = "关键字(模糊查询)" ,name = "keyword",paramType = "query",dataType = "String"))
    public SingleResult list(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字搜索",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页大小",dataType = "String",paramType = "query")})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        if (StringUtils.isNotBlank(startDate))
            map.put("startDate", startDate);
        if (StringUtils.isNotBlank(endDate))
            map.put("endDate", endDate);
        map.put("keyword", keyword.trim());
        map.put("current", current);
        map.put("pageSize", size);

        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        return service.getListPage(map);
    }


}

